Spring Boot JPA ব্যবহার করার সময় Exception Handling এবং Logging অত্যন্ত গুরুত্বপূর্ণ, কারণ এটি অ্যাপ্লিকেশনের ডিবাগিং এবং ত্রুটি সনাক্তকরণে সাহায্য করে। Spring Boot এর মাধ্যমে আপনি সহজে exception handling এবং logging পরিচালনা করতে পারেন, যা আপনার অ্যাপ্লিকেশনকে আরও স্থিতিশীল এবং ট্রেসযোগ্য করে তোলে। নিচে Exception Handling এবং Logging এর ধারণা এবং উদাহরণ দেওয়া হয়েছে।
1. Exception Handling in Spring Boot JPA
Spring Boot JPA-তে exception handling মূলত @ControllerAdvice এবং @ExceptionHandler অ্যানোটেশন ব্যবহার করে করা হয়। এর মাধ্যমে আপনি ডেটাবেস অপারেশন বা অন্যান্য অপারেশন চলাকালীন যেকোনো ধরনের ত্রুটি (exceptions) সেন্ট্রালি হ্যান্ডেল করতে পারেন।
উদাহরণ: Exception Handling with @ControllerAdvice and @ExceptionHandler
Step 1: Custom Exception Class
প্রথমে একটি কাস্টম exception ক্লাস তৈরি করুন যা ডেটাবেস বা অন্যান্য অপারেশনগুলির ত্রুটি নির্ধারণে সাহায্য করবে।
public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(String message) {
super(message);
}
}
Step 2: Service Layer with Exception Handling
ধরা যাক, ProductService ক্লাসে আমরা ProductNotFoundException কাস্টম exception থ্রো করব যখন কোনও পণ্য ডাটাবেসে পাওয়া না যায়।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Product getProductById(Long id) {
Optional<Product> product = productRepository.findById(id);
if (product.isPresent()) {
return product.get();
} else {
throw new ProductNotFoundException("Product not found with id: " + id);
}
}
}
Step 3: Global Exception Handler with @ControllerAdvice
এখন, আমরা @ControllerAdvice ব্যবহার করে একটি global exception handler তৈরি করব, যা অ্যাপ্লিকেশন জুড়ে যে কোন exception হ্যান্ডেল করবে।
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ProductNotFoundException.class)
public ResponseEntity<String> handleProductNotFoundException(ProductNotFoundException ex) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
}
// Handle other exceptions
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleGenericException(Exception ex) {
return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
এখানে, @ControllerAdvice গ্লোবালি সমস্ত exception হ্যান্ডল করতে ব্যবহৃত হচ্ছে এবং @ExceptionHandler নির্দিষ্ট exception (যেমন ProductNotFoundException) হ্যান্ডল করছে।
Step 4: Controller Layer
এখন, আমরা ProductController ক্লাসে ProductService এর মাধ্যমে একটি পণ্য খুঁজে বের করার চেষ্টা করব এবং যদি পণ্য না পাওয়া যায়, তবে কাস্টম exception ফেলা হবে।
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/products/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.getProductById(id);
}
}
এখন, যদি আপনি /products/1 রিকোয়েস্ট করেন এবং সেই id তে কোনো পণ্য না থাকে, তবে ProductNotFoundException হ্যান্ডল হবে এবং একটি 404 Not Found রেসপন্স ফিরিয়ে দিবে।
2. Logging in Spring Boot JPA
Logging হল অ্যাপ্লিকেশনের কার্যকলাপ ট্র্যাক করার একটি গুরুত্বপূর্ণ উপায়। Spring Boot সাধারণভাবে SLF4J (Simple Logging Facade for Java) এবং Logback (Spring Boot এর জন্য ডিফল্ট logging ফ্রেমওয়ার্ক) ব্যবহার করে।
উদাহরণ: Logging in Spring Boot
Step 1: Add Dependencies (If Needed)
Spring Boot ডিফল্টভাবে SLF4J এবং Logback ব্যবহার করে, তবে আপনি যদি অন্য কোন logging ফ্রেমওয়ার্ক ব্যবহার করতে চান, তবে পমে ডিপেনডেন্সি যোগ করতে পারেন।
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Step 2: Add Logging in Service Layer
Spring Boot-এ SLF4J logger ব্যবহার করতে আপনি LoggerFactory ক্লাসের মাধ্যমে logger তৈরি করতে পারেন এবং কাস্টম লগ বার্তা লিখতে পারেন।
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
private static final Logger logger = LoggerFactory.getLogger(ProductService.class);
@Autowired
private ProductRepository productRepository;
public Product getProductById(Long id) {
logger.info("Fetching product with id: {}", id);
Optional<Product> product = productRepository.findById(id);
if (product.isPresent()) {
logger.info("Product found: {}", product.get().getName());
return product.get();
} else {
logger.error("Product not found with id: {}", id);
throw new ProductNotFoundException("Product not found with id: " + id);
}
}
}
Step 3: Configure Logging Level (Optional)
Spring Boot-এ application.properties বা application.yml ফাইলের মাধ্যমে লগিং লেভেল কনফিগার করা যায়।
# Set logging level for the application
logging.level.com.example=DEBUG
# Set root logging level
logging.level.root=INFO
এখানে, com.example প্যাকেজের জন্য লগ লেভেল DEBUG করা হয়েছে এবং root লগ লেভেল INFO করা হয়েছে।
Step 4: View Logs
Spring Boot-এর ডিফল্টভাবে Logback কনফিগারেশনের মাধ্যমে, আপনি লগগুলি console-এ দেখতে পারবেন। আপনি চাইলে কনফিগারেশন ফাইল ব্যবহার করে লগ ফাইলও তৈরি করতে পারেন।
সারাংশ
Exception Handling এবং Logging Spring Boot JPA-এর গুরুত্বপূর্ণ অংশ যা অ্যাপ্লিকেশনের কার্যকারিতা এবং স্থিতিশীলতা নিশ্চিত করতে সাহায্য করে। Exception Handling এর মাধ্যমে আপনি ডেটাবেস বা অন্যান্য অপারেশনে ঘটে যাওয়া ত্রুটিগুলি সঠিকভাবে হ্যান্ডল করতে পারেন, এবং Logging এর মাধ্যমে অ্যাপ্লিকেশনের কার্যকলাপ ট্র্যাক করতে পারেন। Spring Boot এর @ControllerAdvice এবং @ExceptionHandler এর মাধ্যমে Exception Handling এবং SLF4J এর মাধ্যমে Logging সহজে এবং কার্যকরীভাবে পরিচালনা করা সম্ভব।
Read more